package de.davvid.calendar;

import javax.microedition.pim.*;

import de.davvid.core.Settings;

public class CalendarEvent {

	// Stores data to Calendar
	public static void addEvent(Settings s) {
		try {
			// Get Calendar
			EventList events = (EventList) PIM.getInstance().openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE);
		
			// Check Summary
			if (s.summary.length() == 0) s.summary = "unknown";

			// Create new Entry
			Event event = events.createEvent();
			// Add Field
			if (events.isSupportedField(Event.NOTE)) {
				event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, s.summary);
				event.addString(Event.NOTE, PIMItem.ATTR_NONE, s.note);
			} else {
				event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, s.summary + " - " + s.note);
			}
			if (s.location.length() > 0 && events.isSupportedField(Event.LOCATION)) {
				event.addString(Event.LOCATION, PIMItem.ATTR_NONE, s.location);
			}
			event.addDate(Event.START, PIMItem.ATTR_NONE, s.startDate.getTime());
			event.addDate(Event.END, PIMItem.ATTR_NONE, s.endDate.getTime());

			if(s.category.length() > 0 && events.getCategories().length > 0) {
				if(!events.isCategory(s.category)) events.addCategory(s.category);
				event.addToCategory(s.category);
			}
			
			//Save Entry
			event.commit();
			events.close();

		} catch (UnsupportedFieldException e) {
		    e.printStackTrace();

		} catch (PIMException e) {
		    e.printStackTrace();
		}
	}

}
